﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using Application.MainModule.PhonesManagement;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public List<Phone> GetPhoneByUserId(Guid userId)
        {
            try
            {
                IPhoneManagementService phoneService = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();

                return phoneService.FindPhoneByUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public Phone GetPhoneById(Guid phoneId)
        {
            try
            {
                IPhoneManagementService phoneService = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();

                return phoneService.FindPhoneById(phoneId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangePhone(Phone phone)
        {
            try
            {
                IPhoneManagementService phoneService = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
                phoneService.ChangePhone(phone);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemovePhone(Phone phone)
        {
            try
            {
                IPhoneManagementService phoneService = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
                phoneService.RemovePhone(phone);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddPhone(Phone phone)
        {
            try
            {
                IPhoneManagementService phoneService = IoCFactory.Instance.CurrentContainer.Resolve<IPhoneManagementService>();
                phoneService.AddPhone(phone);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
